package com.kuang.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/jdbc")
public class JdbcController {
    @Autowired
    JdbcTemplate jdbcTemplate;
    @GetMapping("/list")
    public List<Map<String,Object>> userList(){
        String sql="select * from employee";
        List<Map<String, Object>> maps = jdbcTemplate.queryForList(sql);
        return maps;
    }
    @GetMapping("/add")
    public String addUser(){
        String sql="insert into employee(lastName,email,gender,department,birth)"+
        "values ('狂生活','869285457@qq.com',1,101,'"+new Date().toLocaleString()+"')";
        jdbcTemplate.update(sql);
        return "addOK";

    }
    @GetMapping("/update/{id}")
    public String updateUser(@PathVariable("id") int id){
        String sql="update employee set lastName=?,email=? where id="+id;
        Object[] objects = new Object[2];
        objects[0]="林东松";
        objects[1]="100000@qq.com";
        jdbcTemplate.update(sql,objects);
        return "updateOk";
    }
    @GetMapping("/delete/{id}")
    public String delUser(@PathVariable("id") int id){
        String sql="delete from employee where id="+id;
        jdbcTemplate.update(sql);
        return "deleteok";
    }
}
